package org.jeecg.modules.friends.fetch.youzan;

import org.jeecg.common.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.youzan.cloud.open.sdk.common.exception.SDKException;
import com.youzan.cloud.open.sdk.core.client.core.DefaultYZClient;
import com.youzan.cloud.open.sdk.core.oauth.model.OAuthToken;
import com.youzan.cloud.open.sdk.core.oauth.token.TokenParameter;

/**
 * @author cold
 * @data 2024/6/21 23:27
 * @version 1.0
 */

@Service
public class YouzanTokenFetch {
    public static final String clientId = "7964bf355626a10039";
    public static final String clientSecret = "7e5b984a76dae11f5064039b1c1eee93";
    public static final String ktdId = "165076452";

    @Autowired
    private RedisUtil redisUtil;
    public String getYouZToken() {
        if (redisUtil.hasKey("youcan_token_" + ktdId)) {
            String token = (String) redisUtil.get("youcan_token_" + ktdId);
            OAuthToken authToken = JSON.parseObject(token, OAuthToken.class);
            if (authToken.getExpires() <= System.currentTimeMillis()) {
                OAuthToken oAuthToken = getToken();
                Long expireSecond = (oAuthToken.getExpires() - System.currentTimeMillis()) / 1000;
                redisUtil.set("youcan_token_" + ktdId, JSON.toJSONString(oAuthToken), expireSecond);
                return oAuthToken.getAccessToken();
            }
            return authToken.getAccessToken();
        } else {
            OAuthToken oAuthToken = getToken();
            Long expireSecond = (oAuthToken.getExpires() - System.currentTimeMillis()) / 1000;
            redisUtil.set("youcan_token_" + ktdId, JSON.toJSONString(oAuthToken), expireSecond);
            return oAuthToken.getAccessToken();
        }
    }

    private OAuthToken getToken() {
        try {
            DefaultYZClient yzClient = new DefaultYZClient();
            TokenParameter tokenParameter = TokenParameter.self()
                    .clientId(clientId)
                    .clientSecret(clientSecret)
                    .grantId(ktdId)
                    .refresh(false)
                    .build();
            OAuthToken oAuthToken = yzClient.getOAuthToken(tokenParameter);
            return oAuthToken;
        } catch (SDKException e) {
            throw new RuntimeException(e);
        }
    }

    public static void main(String[] args) {
        try {
            DefaultYZClient yzClient = new DefaultYZClient();
            TokenParameter tokenParameter = TokenParameter.self()
                    .clientId(clientId)
                    .clientSecret(clientSecret)
                    .grantId(ktdId)
                    .refresh(false)
                    .build();
            OAuthToken oAuthToken = yzClient.getOAuthToken(tokenParameter);
            Long expireSecond = (oAuthToken.getExpires() - System.currentTimeMillis()) / 1000;
            System.out.println("过期时间"+ expireSecond + "json:" + JSON.toJSONString(oAuthToken));
        } catch (SDKException e) {
            throw new RuntimeException(e);
        }
    }
}
